package com.rosense.client.rpc;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.rmi.RemoteException;

import com.rosense.server.Version;
import com.rosense.server.config.HostBean;
import com.rosense.server.config.HostCaches;

/**
 * 
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月23日 下午3:16:11
 *
 */
public class RPCServerLinux extends RPCServer<RPCBean> {

	public RPCServerLinux() throws RemoteException {
		super();
	}

	@Override
	public RPCBean get() throws RemoteException {
		RPCBean bean = new RPCBean();
		bean.version = Version.version;
		return bean;
	}

	@Override
	public RpcJarBean getJar() {
		RpcJarBean jarBean = new RpcJarBean();
		RandomAccessFile raf = null;
		byte[] bytes = null;
		try {
			raf = new RandomAccessFile(HostCaches.jarPath, "r");
			bytes = new byte[(int) raf.length()];
			raf.read(bytes);
			jarBean.setBytes(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return jarBean;
	}

	@Override
	public void set(RPCBean bean) throws RemoteException {
		HostBean hostBean = HostCaches.hostMap.get(bean.ip);
		if (hostBean != null)
			hostBean.setRpcBean(bean);
	}

}
